<?php
include_once "baseChannel.php";

class hugo extends baseChannel
{
    public $api_user_name;
    public $cert;
    public $ip;
    public $header;

    public function __construct($setting)
    {
        parent::__construct($setting);
        $this->api_user_name = $setting['api_user_name'];
        $this->cert = $setting['cert'];
        $this->ip = $setting['ip'] ?? $_SERVER['SERVER_ADDR'];;
    }
    /*通用签名方法*/
    public function getSign($params)
    {
        return md5($this->ip . $this->timestamp . $this->cert);
    }
    public function initHeader($api_name)
    {
        $this->header = [
            'Content-Type: application/json',
            'API-USER-NAME: ' . $this->api_user_name,
            'API-NAME: ' . $api_name,
            'API-TIMESTAMP: ' . $this->timestamp,
            'API-SIGNATURE: ' . $this->getSign([])
        ];
    }
    /*查询余额*/
    public function queryBalance($params = [])
    {
        $url = $this->api_url;
        $this->initHeader('BalanceQuery');
        $post_data = [];
        $return_str = $this->httpRequest($url, 'POST', json_encode($post_data), '', '', $this->header);
        $return_data = json_decode($return_str, 1);
        if ($return_data['ack'] == 'success') {
            return [
                'error' => 0,
                'msg' => '查询成功',
                'balance' => $return_data['balance'] ?? 0, //余额
                'credit' => 0, //授信
                'code' => 200
            ];
        }
        return [
            'error' => 1,
            'msg' => $return_data['message'] ?? '查询失败',
            'code' => 500
        ];
    }
    public function queryProduct($params = [])
    {
        return [
            'error' => -1,
            'msg' => '不支持queryProduct',
            'code' => 500
        ];
    }
    public function queryOrder($params)
    {
        $url = $this->api_url;
        $this->initHeader('OrderQuery');
        $post_data = [
            'order_number' => $params['channel_order_sn'],
            // 'tradeNo' => $params['push_order_sn'],
        ];
        $return_str = $this->httpRequest($url, 'POST', json_encode($post_data), '', '', $this->header);
        $return_data = json_decode($return_str, 1);

        $ret = [
            'error' => 0,
            'order_status' => 2,
            'msg' => '初始化',
            'voucher' => $return_data['voucher'] ?? '', //凭证
            'post_data' => json_encode($post_data), //提交字符串
            'channel_data' => $return_str, //返回字符串
            'code' => 200
        ];
        //充值中
        $ret['msg'] = '充值中';

        if ($return_data['ack'] == 'success') {
            $ret['msg'] = '充值中';
            // var_dump($return_data['order']['shipping_status_desc']);
            // $msg = $return_data['order']['shipping_status_desc'];
            // . $msg;
            // .$msg;
            if (in_array($return_data['order']['shipping_status'], [0, 4])) {
                $ret['order_status'] = self::OrderFail;
                $ret['msg'] = '充值失败:';
            }
            if ($return_data['order']['shipping_status'] == 1) {
                $ret['order_status'] = self::OrderSuccess;
                $ret['msg'] = '充值完成:';
            }
        }

        return $ret;
    }
    public function submitOrder($mobile, $code, $params = [])
    {
        $url = $this->api_url;
        $this->initHeader('OrderCreate');
        $post_data = [
            'phone' => $mobile,
            'product_id' => $code,
            'notify_url' => $this->callback_url,
            'tradeNo' => $params['push_order_sn'],
            'type' => $params['isp'],
        ];
        $return_str = $this->httpRequest($url, 'POST', json_encode($post_data), '', '', $this->header);
        $return_data = json_decode($return_str, 1);

        $ret = [
            'error' => -1,
            'msg' => '初始化',
            'channel_order' => $return_data['message']['order_number'] ?? '',
            'post_data' => json_encode($post_data),
            'channel_data' => $return_str,
            'code' => 200,
            'channel_order_price' => $return_data['message']['charged_amount'] ?? 0,
            'channel_balance' => null,
        ];

        if ($return_data['ack'] == 'success' && $return_data['message']['shipping_status'] == 5) {
            $ret['error'] = 0;
            $ret['msg'] = '下单成功';
        }
        if ($return_data['ack'] == 'success' && $return_data['message']['shipping_status'] != 5) {
            $ret['error'] = 1;
            $ret['code'] = 500;
            $ret['msg'] = $return_data['message']['shipping_status_desc'] ?? '下单失败';
        }
        //下单失败 "message":"余额不足"
        if ($return_data['ack'] == 'failure') {
            $ret['error'] = 1;
            $ret['code'] = 500;
            $ret['msg'] = $return_data['message']['shipping_status_desc'] ?? '下单失败';
        }
        return $ret;
    }

    public function checkCallbackSign($params)
    {
        $str = "{$params['order_number']}{$params['shipping_status']}{$params['tradeNo']}{$this->cert}";
        return md5($str);
    }
    public function callback($params)
    {
        $this->callback_params = $params;
        //        $params = json_decode($params,1);
        $this->channel_order_sn = trim($params['order_number']);
        $this->push_order_sn = $params['tradeNo'];
        //返回参数
        $res = $this->initCallBackParam();
        //订单状态
        $order_status = $params['shipping_status'];
        $msg = $params['shipping_status_desc'];
        //凭证
        $pz = $params['voucher'];
        $res['voucher'] = $pz;

        $sign = $this->checkCallbackSign($params);

        if ($sign != $params['sign']) {
            $res['error'] = 1;
            $res['msg'] = '签名不匹配';
            $res['resp_data'] = 'fail';
            return $res;
        }

        if ($order_status == 1) {
            $res['order_status'] = self::OrderSuccess;
            $res['msg'] = $msg ?? '充值完成';
        }

        if ($order_status == 4) {
            $res['order_status'] = self::OrderFail;
            $res['msg'] = $msg ?? '充值失败';
        }
        return $res;
    }
}
